<template>
  <div class="cart">
    <div v-if="cart.length">
      <van-swipe-cell
        v-for="prod in cart"
        :key="prod.id"
      >
        <van-card
          :price="prod.price"
          :title="prod.title"
          class="goods-card"
        >
          <template #num>
            <van-stepper
              theme="round"
              button-size="22"
              integer
              :value="prod.amount"
              @change="changeAmount({id: prod.id, amount: $event})"
            />
          </template>
          <template #thumb>
            <div class="van-image" style="width: 100%; height: 100%; display: flex;">
              <van-checkbox
                :value="prod.isChecked"
                @click="toggleChecked(prod.id)"
              ></van-checkbox>
              <img :src="prod.image" class="van-image__img" style="width: 60px;">
            </div>
          </template>
        </van-card>
        <template #right>
          <van-button
            square
            text="删除"
            type="danger"
            class="delete-button"
            @click="removeFromCart(prod.id)"
          />
        </template>
      </van-swipe-cell>
    </div>
    <div v-else>
      <van-empty description="购物车为空" />
    </div>

    <van-submit-bar :price="totalMoney" button-text="提交订单" @submit="$router.push('/confirm')">
      <van-checkbox :value="isAllChecked" @click="toggleCheckAll(isAllChecked)">全选</van-checkbox>
      <template #tip>
        你的收货地址不支持同城送, <span>修改地址</span>
      </template>
    </van-submit-bar>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'Cart',
  computed: {
    ...mapState(['cart']),
    ...mapGetters(['isAllChecked', 'totalMoney'])
  },
  methods: {
    ...mapMutations(['removeFromCart', 'changeAmount', 'toggleChecked', 'toggleCheckAll'])
  }
}
</script>

<style lang="less" scoped>
  .goods-card {
    margin: 0;
    background-color: #fff;
  }

  .delete-button {
    height: 100%;
  }
</style>
